package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam(name = "page") Integer page, @RequestParam(name = "size") Integer size) {
        //分页条件
        Page<Item> itemPage = new Page<>(page,size);
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper();
        //不查询逻辑删除 的数据
        queryWrapper.ne(Item::getStatus, 3);
        queryWrapper.orderByDesc(Item::getId);
        Page<Item> page1 = itemService.page(itemPage, queryWrapper);

        return new PageDTO<>(page1.getTotal(),page1.getRecords());
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findByPage")
    public PageDTO<ItemDTO> findByPage4Search(
            @RequestParam(name = "page") int page,
            @RequestParam(name = "size") int size) {
        //分页条件
        Page<Item> itemPage = new Page<>(page,size);

        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper();
        //只查询正常的数据
        queryWrapper.eq(Item::getStatus, 1);
        queryWrapper.orderByAsc(Item::getId);

        Page<Item> page1 = itemService.page(itemPage, queryWrapper);

        List<Item> itemList = page1.getRecords();

        List<ItemDTO> itemDTOList = new ArrayList<>();
        for (Item item : itemList) {
            ItemDTO itemDTO = new ItemDTO();
            BeanUtils.copyProperties(item, itemDTO);
            itemDTOList.add(itemDTO);
        }

        return new PageDTO<ItemDTO>(page1.getTotal(),itemDTOList);
    }

    /**
     * 新增
     * @param item
     * @return
     */
    @PostMapping
    public String addItem(@RequestBody Item item) {
        if (StringUtils.isEmpty(item.getName())) {
            throw new RuntimeException("名称不能为空");
        }
        item.setStatus(1);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
        return "新增成功";
    }

    /**
     * 商品上/下架
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable(name = "id") Long id, @PathVariable(name = "status") Integer status) {

        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        item.setUpdateTime(new Date());

        itemService.updateById(item);
        //发送消息
        String routingkey = status == 1 ? "item.up" : "item.down";

        rabbitTemplate.convertAndSend("item.topic",routingkey,id.toString());
    }

    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @PutMapping
    public String update(@RequestBody Item item) {
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        return "修改成功";
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String delete(@PathVariable("id") Long id) {
        Item item = itemService.getById(id);
        item.setStatus(3);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
        return "删除成功";
    }

    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public ItemDTO getById(@PathVariable(name = "id")Long id){
        Item item = itemService.getById(id);
        ItemDTO itemDTO = new ItemDTO();
        BeanUtils.copyProperties(item,itemDTO);
        return itemDTO;
    }
    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item queryById(@PathVariable(name = "id")Long id){
        Item item = itemService.getById(id);
        return item;
    }


    /**
     * 修改库存
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable(name = "itemId") Long itemId,
                            @PathVariable(name = "num") Integer num){
        itemService.deductStock(itemId, num);
    }

}
